package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Salesman;
import com.example.service.SalesmanService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("salesmans")
public class SalesmanController {
    

    @Autowired
    private SalesmanService salesmanService;

    /**
     * 销售员登录
     * @return
     */
    @PostMapping("/loginSalesman")
    public R login(HttpServletRequest request, @RequestBody Salesman salesman){

        LambdaQueryWrapper<Salesman> lqw= new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(salesman.getJobNumber()),Salesman::getJobNumber,salesman.getJobNumber());

        Salesman salesman1 = salesmanService.getOne(lqw);

        if (salesman1 != null) {
            return R.success("登录成功！",salesman1);

        }else {
            return R.error("登录失败！");
        }
    }

    /**
     * 分页查询所有销售员信息
     * @return
     */

    @GetMapping("/getPageSalesman/{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,Salesman salesman){

        IPage page=new Page(currentPage,pageSize);
        LambdaQueryWrapper<Salesman> lqw =new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(salesman.getJobNumber()),Salesman::getJobNumber,salesman.getJobNumber());

        IPage page1 = salesmanService.page(page,lqw);


        return R.success("分页查询所有salesmans成功！",page1);
    }



//    /**
//     * 查询当前销售员
//     * @return
//     */
//
//    @GetMapping("/getMyself/{id}")
//
//    public R getOne(@PathVariable int id){
//
//        Salesman salesman= salesmanService.getOne(id);
//
//        return R.success("分页查询所有salesman成功！",salesman);
//    }
//
//
    /**
     * 添加
     * @param salesman
     * @return
     */
    @PostMapping("/addSalesman")
    public R add(@RequestBody Salesman salesman){

        if (salesman.getName()==null){
            return R.error("添加资料有误！添加失败！");
        }

        LambdaQueryWrapper<Salesman> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(salesman.getJobNumber()),Salesman::getJobNumber,salesman.getJobNumber());
        Salesman salesman1=salesmanService.getOne(lqw);

        if (salesman1==null) {
            Boolean update = salesmanService.save(salesman);
            if (update) {
                return R.success("添加salesman成功!", null);
            }
        }
        return R.error("工号已存在");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteSalesman/{id}")
    public R deleteById(@PathVariable int id){

        Boolean byId = salesmanService.removeById(id);
        if (byId){
            return R.success("删除salesmans成功!",null);
        }
        return R.error("删除salesmans失败!");
    }

    /**
     * 修改
     * @param salesman
     * @return
     */
    @PutMapping("/updateSalesman")
    public R deleteById(@RequestBody Salesman salesman){

        Boolean update = salesmanService.updateById(salesman);
        if (update){
            return R.success("修改salesman成功!",salesman);
        }
        return R.error("修改salesman失败!");
    }


}

